package com.chenyaowei.cart.vo;

import com.chenyaowei.cart.bo.CartItemBO;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * 需要计算的属性，必须从写他的get方法，保证每次获取属性进行计算
 */
public class CartVO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 商品列表
     */
    private List<CartItemBO> items;

    /**
     * 商品总数量
     */
    private Integer countNum;

    /**
     * 商品类别总数
     */
    private Integer countType;

    /**
     * 商品总价
     */
    private BigDecimal totalAmount;

    /**
     * 减免价格
     */
    private BigDecimal reduce;

    public List<CartItemBO> getItems() {
        return items;
    }

    public void setItems(List<CartItemBO> items) {
        this.items = items;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    /**
     * 计算总数量
     *
     * @return
     */
    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemBO item : items) {
                if (item.getChecked()) {
                    count += item.getCount();
                }
            }
        }
        return count;
    }

    /**
     * 计算总类别数
     *
     * @return
     */
    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemBO item : items) {
                count += 1;

            }
        }
        return count;
    }

    /**
     * 计算总价格
     *
     * @return
     */
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        //1、计算购物总价
        if (items != null && items.size() > 0) {
            for (CartItemBO item : items) {
                if (item.getChecked()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        //2、减去优惠总价
        return amount.subtract(getReduce() == null? new BigDecimal("0") : getReduce());
    }

}
